package com.soikc.kuaidiE.util;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.File;
import java.sql.*;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import java.util.function.Consumer;

public class DruidUtil {

    private static DataSource ds;
    static{
        File f = new File(DruidUtil.class.getResource("/").getPath());
        System.out.println("配置加载路径:"+ f.getPath());
        try {
            Properties ppt = new Properties();
            ppt.load(DruidUtil.class.getClassLoader().getResourceAsStream("druid.properties"));
            ds = DruidDataSourceFactory.createDataSource(ppt);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 从连接池中取出一个连接给用户
     * @return
     */
    public static Connection getConnection(){
        try {
            return ds.getConnection();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    public static ResultSet ppsQuery(Connection connection,String sql,Object ...args) throws SQLException {
        PreparedStatement pps = ppsPot(connection, sql, args);
        return    pps.executeQuery();
    }
    public static ResultSet ppsQuery(Connection connection, String sql, List list) throws SQLException {
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        for (int i=0;i< list.size();i++){
            preparedStatement.setObject(i+1,list.get(i));
        }
        return preparedStatement.executeQuery();
    }
    public static int ppsUpdate(Connection connection, String sql, List list) throws SQLException {
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        for (int i=0;i< list.size();i++){
            if (list.get(i) instanceof Date){
                preparedStatement.setString(i+1,DateFormatUtil.format((Date) list.get(i)));
                continue;
            }
            preparedStatement.setObject(i+1,list.get(i));
        }
        return preparedStatement.executeUpdate();
    }
    public static int ppsUpdate(Connection connection,String sql,Object ...args) throws SQLException {
        PreparedStatement pps = ppsPot(connection, sql, args);
        return    pps.executeUpdate();
    }
    public static ResultSet query(Connection connection,String sql) throws SQLException{
        Statement statement = connection.createStatement();
        ResultSet resultSet = statement.executeQuery(sql);
        return resultSet;
    }
    public static int update(Connection connection,String sql) throws SQLException {
        Statement statement = connection.createStatement();
        return statement.executeUpdate(sql);
    }
//绑定参数
    private static PreparedStatement ppsPot(Connection connection, String sql, Object[] args) throws SQLException {
        PreparedStatement pps = connection.prepareStatement(sql);
        for (int i=0;i< args.length;i++){
            if (args[i] instanceof String){
                pps.setString(i+1,(String) args[i]);
            }else if (args[i] instanceof Integer){
                pps.setInt(i+1,(Integer) args[i]);
            }else if (args[i] instanceof Date){
                pps.setString(i+1,DateFormatUtil.format((Date) args[i]));
            }else {
                throw new SQLException("传入了未知数据类型");
            }
        }
        return pps;
    }

    public static void close(Connection conn, Statement state, ResultSet rs){
        try {
            rs.close();
            state.close();
            conn.close();
        } catch (Exception e) {

        }
    }
    public static void close(Connection conn){
        try {
            conn.close();
        } catch (Exception err) {

        }
    }
}
